import { Component } from '@angular/core';
import { ListService, PagedAndSortedResultRequestDto } from '@abp/ng.core';
import { IdentityRoleDto, IdentityRoleService } from '../proxy/identity';
import { EntityListComponent } from '../../core';
import { ListBottonsConfigService, ThemeService } from '../../services';
import { IdentityRoleListService } from './user-role.service';
import { RefRoleDetailService } from '../ref-role-detail/ref-role-detail.service';
import { RefPermissionService } from '../ref-permission/ref-permission.service';

@Component({
  selector: 'app-role-edit',
  templateUrl: './user-role.component.html',
  providers: [ListService, ListBottonsConfigService],
})
export class UserRoleComponent extends EntityListComponent<IdentityRoleDto> {
  constructor(
    public readonly list: ListService<PagedAndSortedResultRequestDto>,
    public buttonConfigService: ListBottonsConfigService,
    public identityRoleListService: IdentityRoleListService,
    public theme: ThemeService,
    public roleDetailService: RefRoleDetailService,
    public refPermissionService: RefPermissionService,
    public identityRoleService: IdentityRoleService
  ) {
    super(list, buttonConfigService, identityRoleListService);
  }

  public new() {
    this.roleDetailService.show().subscribe(model => {
      if (!!model) {
        this.identityRoleService.create(model).subscribe(() => this.list.get());
      }
    });
  }

  public view(model: IdentityRoleDto, event?: Event) {
    event?.stopPropagation();
    this.roleDetailService.show(model).subscribe(model => {
      if (!!model) {
        this.identityRoleService.update(model['id'], model).subscribe(() => this.list.get());
      }
    });
  }

  public editPermission(model: IdentityRoleDto, event?: Event) {
    event?.stopPropagation();
    this.refPermissionService.show({ providerName: 'R', keyModel: model }).subscribe();
  }
}
